package io.renren.controller;

import io.renren.constant.AppConstant;
import io.renren.entity.FileResponseInfo;
import io.renren.entity.FileUploadInfo;
import io.renren.service.FileService;
import io.renren.vo.RespModel;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/file")
@Api(tags="文件上传")
public class FileResourceController {

	private static final Logger logger  = LoggerFactory.getLogger(FileResourceController.class);
	
	@Autowired
	private FileService fileService;
	
	@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "文件上传", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {
			@ApiResponse(code = 200, message = "OK", response = RespModel.class),
			@ApiResponse(code = 500, message = "文件上传")}
	)
	@com.cxytiandi.encrypt.springboot.annotation.Decrypt
	@com.cxytiandi.encrypt.springboot.annotation.Encrypt
	public RespModel<FileResponseInfo> uploadfile(@ApiParam("文件") MultipartFile[] images){
        List<FileUploadInfo> results = new ArrayList();
        Arrays.stream(images).forEach(
                (item)->{
                    FileUploadInfo fileUploadInfo = fileService.uploadFile(item);
                    results.add(fileUploadInfo);
                }
        );
		return new RespModel<>().getRespModelInstance(AppConstant.STATU_SUCCESS,"",results);
	}
	/**
	 * 返回结果
	 * {"status":"SUCCESS","errorcode":"000","reason":"SUCCESS","result":{"result":[{"url":"http://211.103.175.219:38086/dfs/group1/M00/00/04/CgIZxlkiltaAGl6EAAIsxxEFvw0572.jpg","name":"0.jpg","status":"SUCCESS"}]}}
	 */
	
//	@PostMapping(value = "/uploadIOS",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
//	@ApiOperation(value = "文件上传", produces = MediaType.APPLICATION_JSON_VALUE)
//	@ApiResponses(value = {
//			@ApiResponse(code = 200, message = "OK", response = RespModel.class),
//			@ApiResponse(code = 500, message = "文件上传")}
//	)
//	public Response uploadIOSFile(@ApiParam(value = "文件") @RequestParam(value="image",required=false) MultipartFile[] images){
//		RespModel resp = fileService.uploadFileIOS(images);
//		logger.info("上传文件返回结果================"+JSON.toJSONString(resp));
//		return Response.status(200).entity(resp).build();
//	}
}
